import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
const Login = () => import('@/views/login/LoginPage.vue')
const LayoutPage = () => import('@/views/layout/LayoutPage.vue')
const ArticleChannel = () => import('@/views/article/ArticleChannel.vue')
const ArticleManage = () => import('@/views/article/ArticleManage.vue')
const UserAvator = () => import('@/views/user/UserAvator.vue')
const UserProfile = () => import('@/views/user/UserProfile.vue')
const UserPassword = () => import('@/views/user/UserPassword.vue')
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: LayoutPage,
      redirect: '/article/channel',
      children: [
        {
          path: 'article/channel',
          component: ArticleChannel
        },
        {
          path: 'article/manage',
          component: ArticleManage
        },
        {
          path: 'user/avator',
          component: UserAvator
        },
        {
          path: 'user/profile',
          component: UserProfile
        },
        {
          path: 'user/password',
          component: UserPassword
        }
      ]
    }
  ]
})

// 添加全局导航守卫
router.beforeEach(async (to) => {
  const token = useUserStore().token
  if (!token && to.path !== '/login') {
    return { path: '/login' }
  }
})
export default router
